<?php

class Admin_Model_User extends Zend_Db_Table_Abstract
{
    protected $_name = 'user';
    
    public function getAllData(){
		$query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from($this->_name);
        return $this->fetchAll($query);
    }    
    public function getDataByUserId($id){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from($this->_name)
                        ->where('userId = ?',(int)$id);
        return $this->fetchRow($query);
    }
    public function getDataCheckUnique($data){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from($this->_name);    
        if(isset($data['email']) && $data['email']){
            $query->where("email = '$data[email]'");
        }                     
        if(isset($data['username']) && $data['username']){
            $query->orWhere("username = '$data[username]'");
        }                                        
        if(isset($data['mobile']) && $data['mobile']){
            $query->orWhere("mobile = '$data[mobile]'");
        }                                        
        return $this->fetchRow($query);
    }
    public function getDataByUsername($username){
        $query = $this->select()
            ->from($this->_name)
            ->where('username = ?', $username);
        return $this->fetchRow($query);
    }
    public function getDataByEmail($email){
        $query = $this->select()
            ->from($this->_name)
            ->where('email = ?', $email);
        return $this->fetchRow($query);
    }
    public function getDataByMobile($mobile){
        $query = $this->select()
            ->from($this->_name)
            ->where('mobile = ?', $mobile);
        return $this->fetchRow($query);
    }
    public function addData($data){        
        $data = array('username'    => $data['username'],
                'email'             =>$data['email'],
                'mobile'            =>$data['mobile'],
                'phone'            =>$data['phone'],
                'fullname'          =>$data['fullname'],
                'birth'          =>$data['birth'],
                'sex'          =>$data['sex'],
                'address'          =>$data['address'],
                'role'              =>$data['role'],
                'status'            =>$data['status'],                
                'gold'              =>$data['gold'],
                );
        $idAdd = $this->insert($data);        
        return $idAdd;
    }
    public function updateData($userId = '',$data){        
        $data = array('username'    => $data['username'],                
                'email'             =>$data['email'],
                'mobile'            =>$data['mobile'],
                'phone'            =>$data['phone'],
                'fullname'          =>$data['fullname'],
                'birth'          =>$data['birth'],
                'sex'          =>$data['sex'],
                'address'          =>$data['address'],
                'role'              =>$data['role'],
                'status'            =>$data['status'],
                'avatar'              =>$data['avatar'],
                'gold'              =>$data['gold']);
        return $this->update($data, 'userId = '.(int)$userId);
    }            
    public function deleteData($userId){
        $this->delete('userId = '.(int)$userId);
    }    
    public function updateStatus($userId){
        $this->update(array('status'=>'1'),'userId = '.$userId);                        
    }
    public function updatePass($userId, $password){
        $data['password'] = md5($password);
        return $this->update($data,'adminId='.(int)$userId);
    }
    public function getUserFreeCom(){
        $modelCompany=new Admin_Model_Company();
        $companyUser = $modelCompany->select()
            ->from($modelCompany->info(Zend_Db_Table_Abstract::NAME) ,array('userId'));                                                                   
        $query = $this->select()
            ->from($this->_name)
            ->where('role = 1')
            ->where('userId NOT IN (?)',$companyUser);
        $kq=$this->fetchAll($query);                                               
        return $this->fetchAll($query);
    }       
    
}